﻿package code {
	// class gives players selection menu
	
	import flash.display.MovieClip;
	import com.as3toolkit.events.KeyboarderEvent;
	import com.as3toolkit.ui.Keyboarder;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import caurina.transitions.Tweener;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	
	public class GUI extends MovieClip{
		private var key1:int;
		private var key2:int;
		private var key3:int;
		private var key4:int;
		private var key5:int;
		private var key6:int;
		private var key7:int;
		private var key8:int;
		private var horzCounter, vertCounter:int;
		
		private var levelNum:int;
		private var keyInfo:String;
		private var owner:Player;
		private var player:int;
		
		private var cardLayer:Sprite;
		
		public function GUI(playerNum:int, player:Player) {
			setUpKeys(playerNum);
			owner = player;
			cardLayer = new Sprite();
			addChild(cardLayer);
			levelNum = 1;
			keyInfo = "S";
		}
		
		// sets up key bindings based on player
		public function setUpKeys(playerNum:int){
			player = playerNum;
			
			if(playerNum == 1){
				key1 = 81; // q
				key2 = 87; // w
				key3 = 69;  // e
				key4 = 82;  // r
				key5 = 65;  // a
				key6 = 68; // d
				key7 = 83; // s
				key8 = 70; // f
			}
			else{
				key1 = 85; // u
				key2 = 73; // i
				key3 = 79; // o
				key4 = 80; // p
				key5 = 74; // j
				key6 = 76; // l
				key7 = 75; // k
				key8 = 186; // ;
			}

		}
		
		private function getLetter(keyCode:int):String{
			switch(keyCode){
				case 81: return "Q";
				break;
				
				case 87: return "W";
				break;
				
				case 69: return "E";
				break;
				
				case 82: return "R";
				break;
				
				case 65: return "A";
				break;
				
				case 68: return "D";
				break;
				
				case 83: return "S";
				break;
				
				case 70: return "F";
				break;
				
				case 85: return "U";
				break;
				
				case 73: return "I";
				break;
				
				case 79: return "O";
				break;
				
				case 80: return "P";
				break;
				
				case 74: return "J";
				break;
				
				case 76: return "L";
				break;
				
				case 75: return "K";
				break;
				
				case 186: return ";";
				break;
			}
			return ""
		}
		
		public function clearCards(){
			Tweener.addTween(cardLayer, { y: 600, alpha:1, time:.5, transition:"easeInQuint", onComplete:clear} );
		}
		
		private function clear():void{
			if(cardLayer.numChildren > 0){
				var k:int = cardLayer.numChildren;
				while( k > 0 )
				{
					cardLayer.removeChildAt( k - 1 );
					k--;
				}
			}
		}
		
		private function showCards(array:Array):void{
			cardLayer.y += 500;
			
			for(var i:int = 0; i < array.length; i++){
				var temp:CardDisplay3 = new CardDisplay3();
				var tempImage:Bitmap = array[i].getSmallCard();
				
				switch(i){
					case 0: temp.key_txt.text = getLetter(key1);
					break;
					
					case 1: temp.key_txt.text = getLetter(key2);
					break;
					
					case 2: temp.key_txt.text = getLetter(key3);
					break;
					
					case 3: temp.key_txt.text = getLetter(key4);
					break;
					
					case 4: temp.key_txt.text = getLetter(key5);
					break;
					
					case 5: temp.key_txt.text = getLetter(key6);
					break;
					
					case 6: temp.key_txt.text = getLetter(key7);
					break;
				}
				
				temp.name_txt.text = array[i].CardName;
				temp.description_txt.text = array[i].Description;
				temp.cost_txt.text = array[i].ManaCost;
				temp.acc_txt.text = array[i].Accuracy;
				temp.eff_txt.text = array[i].Effect;
				temp.mod_txt.text = array[i].Modifier;
				
				temp.x = 80;
				temp.y = 620 - i * 50;
				cardLayer.addChild(temp);
				
				tempImage.x = temp.x - 25;
				tempImage.y = temp.y + 10;
				cardLayer.addChild(tempImage);
			}
			
			Tweener.addTween(cardLayer, { y: 0, alpha:1, time:.8, transition:"easeOutQuint" } );
		}
		
		// allows player to select cards they want to play
		public function selectCard(keyCode:int){
			if(levelNum == 1){
				if(keyCode == key1){
					keyInfo = "S";
					levelNum++;
					showCards(owner.StoredCards);
				}
				else if(keyCode == key2){
					keyInfo = "B";
					levelNum++;
					showCards(owner.BasicCards);
					
				}
				else if(keyCode == key3){
					keyInfo = "H";
					levelNum++;
					showCards(owner.HandCards);
				}
			}
			else if(levelNum == 2){
				var index:int = 0;
				switch(keyCode){
					case key1: index = 1;
						break;
					case key2: index = 2;
						break;
					case key3: index = 3;
						break;
					case key4: index = 4;
						break;
					case key5: index = 5;
						break;
					case key6: index = 6;
						break;
					case key7: index = 7;
						break;
				}
				if(keyCode == key8){ // allows player to go back
					levelNum = 1;
					clearCards();
				}	
				else if(index >  0){
					if(keyInfo == "S"){
						if(owner.PoweredUp && index <= owner.StoredCards.length){ // can only play if powered up
							trace("here");
							if(checkMana(owner.StoredCards[index-1]) && (owner.IsAttacking && owner.StoredCards[index - 1].Type != "defense") // checks to make sure the player is not using a card they cannot use
							   || ((!owner.IsAttacking && owner.StoredCards[index - 1].Type != "attack") || (!owner.IsAttacking && owner.StoredCards[index - 1].Type != "attackN"))){ 
								owner.chooseCard(index - 1, "stored");  
							   }
						}
						levelNum--;
						clearCards();
					}
					else if(keyInfo == "B"){
						if(index <= owner.BasicCards.length){
							if(checkMana(owner.BasicCards[index - 1]) && (owner.IsAttacking && owner.BasicCards[index - 1].Type != "defense") 
							   || (!owner.IsAttacking && owner.BasicCards[index - 1].Type != "attack")){
								owner.chooseCard(index - 1, "basic");
							   }
						}
						levelNum--;
						clearCards();
					}
					else if(keyInfo == "H"){
						if(index <= owner.HandCards.length){
							if(checkMana(owner.HandCards[index - 1]) && (owner.IsAttacking && owner.HandCards[index - 1].Type != "defense") 
							   || ((!owner.IsAttacking && owner.HandCards[index - 1].Type != "attack") || (!owner.IsAttacking && owner.HandCards[index - 1].Type != "attackN"))){
								owner.chooseCard(index - 1, "hand");
							   }
						}
						levelNum--;
						clearCards();
					}
					
				}
			}
		}
		
		public function checkMana(testCard:Card):Boolean{
			if(owner.Mana >= testCard.ManaCost){
				return true;
			}
			return false;
		}

	}
	
}
